<template>
	<sc-form-table v-model="list" :addTemplate="addTemplate" drag-sort placeholder="暂无数据"
				   :hideAddButton="bpmLinkFlag!='2'" :hideDelButton="bpmLinkFlag!='2'" :dragSort="bpmLinkFlag=='2'">
		<el-table-column prop="id" label="流水号" width="150">
			<template #default="scope">
				<el-input v-model="scope.row.id" placeholder="流水号" @change="setValue(scope.row,scope.$index)"
						  type="number" v-if="bpmLinkFlag=='2'"></el-input>
				<div v-if="bpmLinkFlag!='2'">{{scope.row.id}}</div>
			</template>
		</el-table-column>
		<el-table-column prop="flowTitle" label="流程名称" width="300" :show-overflow-tooltip="true"></el-table-column>
		<el-table-column prop="createUserName" label="创建人" width="150"></el-table-column>
		<el-table-column prop="createTime" label="创建时间" width="150"></el-table-column>
		<el-table-column prop="urgency" label="紧急程度" width="100">
			<template #default="scope">
				<el-tag v-if="scope.row.urgency==0" type="success">一般</el-tag>
				<el-tag v-else-if="scope.row.urgency==1" type="warning">紧急</el-tag>
				<el-tag v-else-if="scope.row.urgency==2" type="danger">加急</el-tag>
				<el-tag v-else type="info">未知</el-tag>
			</template>
		</el-table-column>
		<el-table-column prop="status" label="流程状态" width="100">
			<template #default="scope">
				<el-tag v-if="scope.row.status==0" type="success">进行中</el-tag>
				<el-tag v-else-if="scope.row.status==1" type="warning">已结束</el-tag>
				<el-tag v-else type="info">未知</el-tag>
			</template>
		</el-table-column>
		<el-table-column prop="remark" label="备注说明" min-width="180" :show-overflow-tooltip="true">
			<template #default="scope">
				<el-input v-model="scope.row.remark" placeholder="备注说明" v-if="bpmLinkFlag=='2'" maxlength="380"></el-input>
				<div v-if="bpmLinkFlag!='2'">{{scope.row.remark}}</div>
			</template>
		</el-table-column>
		<el-table-column prop="open" label="操作" width="80" align="center">
			<template #default="scope">
				<el-button-group>
					<el-button text type="primary" size="small" @click="readBpmDetails(scope.row, scope.$index)">查看
					</el-button>
				</el-button-group>
			</template>
		</el-table-column>
	</sc-form-table>
</template>
<script>
export default {
	props: {
		bpmLinkFlag: {type: String},
		runId: {type: String}
	},
	data() {
		return {
			apiObj: '',
			list:[],
			form: {
				runId:this.runId,
				linkBpmStr:'',
			},
			addTemplate: {
				id: null,
				runId: '',
				flowTitle: '',
				createTime:'',
				createUserName:'',
				urgency:'',
				status:'',
				remark: ''
			}
		}
	},
	mounted() {
		this.getBpmLink();
	},
	methods: {
		async setValue(row, index) {
			let res = await this.$API.bpm.bpmList.getBpmById.get({id:row.id,model:'0'});
			if (res.code == 200) {
				if(res.data!=null)
				{
					row.flowTitle=res.data.flowTitle;
					row.runId = res.data.runId;
					row.urgency = res.data.urgency;
					row.status = res.data.status;
					row.createTime = res.data.createTime;
					row.createUserName = res.data.createUserName;
				}
			}
		},
		readBpmDetails(row, index) {
			this.$nextTick(() => {
				this.$router.replace({
					path: '/bpm/handle/read',
					query: {
						runId: row.runId
					}
				})
			})
		},
		async setBpmLink(runId) {
			if(this.list.length>0)
			{
				this.form.linkBpmStr = JSON.stringify(this.list);
			}else {
				this.form.linkBpmStr="";
			}
			this.form.runId = runId;
			await this.$API.bpm.bpmLink.setBpmLink.post(this.form);
		},
		async getBpmLink() {
			let res = await this.$API.bpm.bpmLink.getBpmLink.get({runId: this.runId});
			if (res.code == 200) {
				if(res.data)
				{
					if(res.data.linkBpmStr!=undefined&&res.data.linkBpmStr!="")
					{
						this.form.linkBpmStr = res.data.linkBpmStr;
						this.list = JSON.parse(res.data.linkBpmStr);
					}
				}
				if(this.list.length==0&&this.bpmLinkFlag!="2")
				{
					this.$emit('success',false)
				}
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
	}
}
</script>
